<?php
/**
 * WMI Query Class for PHP 5
 * Version 0.1
 *
 * Requires:
 * - PHP 5.x (http://www.php.net)
 * - WMI Client for Linux (http://www.orvant.com/packages)
 *
 * Written by Armando Ortiz
 *   Email:  aortiz@nointegrity.org
 *
 * Copyright (c) 2012 Armando Ortiz
 *
 * I would appreciate any improvements or additions to be submitted back
 * to benefit all PHP developers who have to deal with Windows network
 * environments.
 *
 * You can find additional information on WMI from the following location:
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa394582(v=vs.85).aspx
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General public License for more details.
 *
 * @category WindowsNetworking
 * @package WMIQuery
 * @author Armando Ortiz
 * @copyright (c) 2012 Armando Ortiz
 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPLv2.1
 * @version 0.1
 * @link http://www.nointegrity.org
 */

/**
* Main WMIQuery class
* 
* Can be initialised using $myWMIQuery = new WMIQuery();
*/
class WMIQuery
{
	// The host to query
	public $wmiQueryHost = null;
	
	// The default nodes to fetch information from
	public $wmiNode = array( "Win32_ComputerSystem", "Win32_OperatingSystem" );
	
	// The default namespace we will query from
	public $wmiNamespace = "root\\cimv2";
	
	// Defines the Windows domain, administrator username and password
	public $wmiDomain	= "WINDOWS_DOMAIN";
	public $wmiUsername	= "adminuser";
	public $wmiPassword	= "adminpassword";

	// Defines what we want back - comma separated fields are accepted here from the WMI resource
	public $wmiFields = "*";
	
	// It doesn't look like the ^ is used very often in return results, so I decided to use it as the default...  :)
	public $wmiClientDelimiter = "^"; 

	// Location of the wmic client - the default install from source is in /bin
	public $wmiClient = "/bin/wmic";

	public function GetNode()
	{
		if( $this->wmiQueryHost != null )
		{
			exec( "$this->wmiClient --namespace=\"$this->wmiNamespace\" --delimiter=\"$this->wmiClientDelimiter\" -U $this->wmiDomain\\\\$this->wmiUsername%$this->wmiPassword //$this->wmiQueryHost \"Select $this->wmiFields from $this->wmiNode\"", $myResults );
		
			$countResults = count( $myResults );
			if( $countResults > 2 )
			{
				$myCountRow = 0;
			
				$myHeaders = explode( $this->wmiClientDelimiter, $myResults[ 1 ] );
				for( $x = 2; $x < $countResults; ++$x )
				{
					$thisRow = explode( $this->wmiClientDelimiter, $myResults[ $x ] );
					for( $y = 0; $y < count( $myHeaders ); ++$y )
					{
						$myNodeResults[ $myCountRow ][ $myHeaders[ $y ] ] = addslashes( $thisRow[ $y ] );
					}
					++$myCountRow;
				}
			}
				else
			{
				$myNodeResults = false;
			}
		
			return $myNodeResults;
		}
			else
		{
			return false;
		}
	}
}
?>
